<?php
/**
 * Created by PhpStorm.
 * User: 睿
 * Date: 2017-11-09
 * Time: 9:12
 */

namespace app\store\controller;

use controller\BasicAdmin;
use service\DataService;
use think\Db;

class Orderr extends BasicAdmin
{
    /**
     * 指定当前数据表
     * @var string
     */
    public $table = 'StoreOrder';

    /**
     * 订货单列表
     * @return array
     */
    public function index()
    {
        $this->title = '订货单管理';
        $get = $this->request->get();

        $store_id = session('user')['store_id'];

        $db = Db::name($this->table)->order('id', 'desc')->where('store_id', $store_id)->where('type',2);
        foreach (['sn'] as $key) {
            (isset($get[$key]) && $get[$key] !== '') && $db->whereLike($key, "%{$get[$key]}%");
        }

        return parent::_list($db);
    }

    public function detail()
    {
        $join = [
            ['ys_goods m', 'a.goods_id=m.id', 'LEFT'],
        ];
        $db = db('store_chengpin_order')->alias('a')->join($join)->where('order_id', input('id'))->select();
        $this->assign('list', $db);

        return $this->_form('store_chengpin_order', 'detail');
    }

    public function rdetail()
    {
        $join = [
            ['ys_goods m', 'a.goods_id=m.id', 'LEFT'],
        ];
        $db = db('store_chengpin_orderr')->alias('a')->join($join)->where('order_id', input('id'))->select();
        $this->assign('list', $db);

        return $this->_form('store_chengpin_orderr', 'form');
    }

    /**
     * 确认收货
     * @return array|string
     * @throws \think\Exception
     */
    public function receive()
    {
        if (request()->ispost()) {
            $post = input();
            foreach ($post as $k => $v) {
                if (strrpos($k, 'num')) {
                    $id = explode('_', $k)[2];
                    $store_material = db('store_chengpin_orderr')->where('id',$id)->find();
                    db('store_chengpin_orderr')->update(['id' => $id, 'rec_num' => $v ,'dif_num' => $v - $store_material['del_num']]);
                    //入库数大于0 
                    if ($v>0){
                        $goods_id = $store_material['goods_id'];
                        $order_id = $store_material['order_id'];
                        $store_order = db('store_order')->where('id',$order_id)->find();
                        $sn = $store_order['sn'];

                        $store_id = session('user')['store_id'];
                        $store_goods = db('store_goods_stock')->where('store_id',$store_id)->where('goods_id',$goods_id)->find();
                        if ($store_goods){
                            db('store_goods_stock')->where('id',$store_goods['id'])->setInc('stock',$v);
                            $stock = $store_goods['stock'] + $v;
                        }
                        else{
                            db('store_goods_stock')->insert([
                                'store_id'=>$store_id,
                                'goods_id' => $goods_id,
                                'stock' => $v,
                                'goods_spec_id' => -1,
                                'addtime' => time()
                            ]);
                            $stock = $v;
                        }

                        $log = [
                            'goods_id' => $goods_id,
                            'goods_spec_id' => -1,
                            'ruku_num' => $v,
                            'remark' => $sn,
                            'addtime' => time(),
                            'cur_stock' => $stock,
                        ];
                        db('store_goods_log')->insert($log);
                    }
                }
            }
            if (db('store_order')->update(['id' => $post['order_id'], 'status' => 3]))
                $this->success('收货成功', '');
        } else {
            $join = [
                ['ys_goods m', 'a.goods_id=m.id', 'LEFT'],
            ];
            $db = db('store_chengpin_orderr')->alias('a')->join($join)->where('order_id', input('id'))->field('a.id,m.name,a.num,a.del_num,a.rec_num,m.unit')->select();

            $this->assign('list', $db);
            $this->assign('order_id', input('id'));

            return $this->_form('store_material_order', 'receive');
        }
    }
}